package com.knowswift.common.utils.getui;

import com.getui.push.v2.sdk.ApiHelper;
import com.getui.push.v2.sdk.GtApiConfiguration;
import com.getui.push.v2.sdk.api.PushApi;
import com.getui.push.v2.sdk.common.ApiResult;
import com.getui.push.v2.sdk.dto.req.Audience;
import com.getui.push.v2.sdk.dto.req.AudienceDTO;
import com.getui.push.v2.sdk.dto.req.Settings;
import com.getui.push.v2.sdk.dto.req.Strategy;
import com.getui.push.v2.sdk.dto.req.message.PushChannel;
import com.getui.push.v2.sdk.dto.req.message.PushDTO;
import com.getui.push.v2.sdk.dto.req.message.PushMessage;
import com.getui.push.v2.sdk.dto.req.message.android.GTNotification;
import com.getui.push.v2.sdk.dto.req.message.ios.Alert;
import com.getui.push.v2.sdk.dto.req.message.ios.Aps;
import com.getui.push.v2.sdk.dto.req.message.ios.IosDTO;
import com.getui.push.v2.sdk.dto.res.TaskIdDTO;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.util.Map;

public class GeTuiUtils {
    public static final String appId = "am6JCV3ZyP7ebr7CYeBpc7";
    public static final String appKey = "44E2AAndng906m5uFaxbB4";
    public static final String appSecret = "gc8UsH5GSq7f7LlnZJ5Dq8";
    public static final String masterSecret = "UqukKIzQ4t7yjEu7Xa4vi2";

    public static PushApi pushApi;

    static {
        init();
    }

    public static void init() {
        GtApiConfiguration apiConfiguration = new GtApiConfiguration();
        apiConfiguration.setAppId(appId);
        apiConfiguration.setAppKey(appKey);
        apiConfiguration.setMasterSecret(masterSecret);
        apiConfiguration.setDomain("https://restapi.getui.com/v2/");
        ApiHelper apiHelper = ApiHelper.build(apiConfiguration);
        // 创建对象，建议复用。目前有PushApi、StatisticApi、UserApi
        pushApi = apiHelper.creatApi(PushApi.class);
    }

    public static ApiResult<Map<String, Map<String, String>>> pushSingle(String title, String body, String cid, boolean isAndroid) {
        if (StringUtils.isBlank(cid)) {
            return null;
        }
        //根据cid进行单推
//        PushDTO<Audience> pushDTO = new PushDTO<>();
        // 设置推送参数
        PushDTO<Audience> pushDTO = PushBuilder.<Audience>create()
                .apply().setRequestId(System.currentTimeMillis() + "")
                .applyPushMessage()
                .withTransmission(body)
                .and()
                .build();
        pushDTO.setRequestId(System.currentTimeMillis() + "");
        Settings settings = new Settings();
        Strategy strategy = new Strategy();
        strategy.setIos(2);
        strategy.setDef(1);
        strategy.setOp(4);
        settings.setStrategy(strategy);
        pushDTO.setSettings(settings);
        GTNotification notification = new GTNotification();
//        if (isAndroid) {
//            pushMessage.setNotification(notification);
//        } else {
//        pushMessage.setTransmission(body);
//        }
        notification.setTitle(title);
        notification.setBody(body);
        notification.setClickType("startapp");
        notification.setChannelLevel("4");
        // 设置接收人信息
        Audience audience = new Audience();
        pushDTO.setAudience(audience);
        audience.addCid(cid);
        PushChannel channel = new PushChannel();
        IosDTO iosDTO = new IosDTO();
        Aps aps = new Aps();
        Alert alert = new Alert();
        alert.setTitle(title);
        alert.setBody(body);
//        alert.setSubtitle(title);
        aps.setAlert(alert);
        aps.setSound("djasndan");
        iosDTO.setAps(aps);
        iosDTO.setPayload(body);
        iosDTO.setType("notify");
        iosDTO.setAutoBadge("+1");
        channel.setIos(iosDTO);
        pushDTO.setPushChannel(channel);

        // 进行cid单推
        return pushApi.pushToSingleByCid(pushDTO);
    }

    public static String test() {
        PushBuilder.<Audience>create().apply().setRequestId("")
                .applyPushMessage().withTransmission(LocalDateTime.now().toString()).and().build();
        //根据cid进行单推
        PushDTO<Audience> pushDTO = new PushDTO<>();
        // 设置推送参数
        pushDTO.setRequestId(System.currentTimeMillis() + "");
        PushMessage pushMessage = new PushMessage();
        pushDTO.setPushMessage(pushMessage);
        GTNotification notification = new GTNotification();
        pushMessage.setTransmission(LocalDateTime.now().toString());
//        pushMessage.setNotification(notification);
//        notification.setTitle("标题" + LocalDate.now());
//        notification.setBody("body");
//        notification.setClickType("startapp");
        // 设置接收人信息
        Audience audience = new Audience();
        pushDTO.setAudience(audience);
        audience.addCid("6f99ac2a49132947b0c568cb172eb177");


        ApiResult<TaskIdDTO> msg = pushApi.createMsg(pushDTO);
        String taskId = msg.getData().getTaskId();

        AudienceDTO audienceDTO = new AudienceDTO();
        audienceDTO.setAudience(audience);
        audienceDTO.setTaskid(taskId);
        AudienceDT audienceDT = new AudienceDT();


        // 进行cid单推
        ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushListByCid(audienceDTO);
        if (apiResult.isSuccess()) {
            // success
            System.out.println(apiResult.getData());
            return apiResult.getData().toString();
        } else {
            // failed
            System.out.println("code:" + apiResult.getCode() + ", msg: " + apiResult.getMsg());
            return "推送失败, msg: " + apiResult.getMsg();
        }
    }

    static class AudienceDT extends AudienceDTO {

    }

    public static void main(String[] args) {
        test();
    }
}
